﻿using KMF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KMF.DAL
{
    public partial class OrderDb
    {
        public static List<OrderPagingDto> GetAllOrderPaging(int page, int itemsPerPage, ref int totalPage)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Order_GetAllPaging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                SqlParameter itemTotalPage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalPage.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<OrderPagingDto>(ref command);
                totalPage = int.Parse(itemTotalPage.Value.ToString() == "" ? "0" : itemTotalPage.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static List<OrderPagingDto> GetAllOrderFilterPaging(int page, int itemsPerPage, ref int totalPage,DateTime s,DateTime e)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Order_GetFilterPaging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                command.Parameters.Add(dao.CreateParameter("@StartDate", s));
                command.Parameters.Add(dao.CreateParameter("@EndDate", e));
                SqlParameter itemTotalPage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalPage.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<OrderPagingDto>(ref command);
                totalPage = int.Parse(itemTotalPage.Value.ToString() == "" ? "0" : itemTotalPage.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static List<OrderDetailProductDto> GetDetailByOrderId(string OrderId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_OrderDetail_GetByPrderId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@OrderId", OrderId));
                var result = dao.GetDTOList<OrderDetailProductDto>(ref command);
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
